package com.ruoyi.appVersion.controller;

import java.util.List;

import cn.hutool.core.date.DateTime;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.appVersion.domain.AppVersion;
import com.ruoyi.appVersion.service.IAppVersionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 脚本版本Controller
 *
 * @author ruoyi
 * @date 2022-06-06
 */
@Controller
@RequestMapping("/appversion/version")
public class AppVersionController extends BaseController {
    private String prefix = "appversion/version";

    @Autowired
    private IAppVersionService appVersionService;

    @RequiresPermissions("appversion:version:view")
    @GetMapping()
    public String version() {
        return prefix + "/version";
    }

    /**
     * 查询脚本版本列表
     */
    @RequiresPermissions("appversion:version:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AppVersion appVersion) {
        startPage();
        List<AppVersion> list = appVersionService.selectAppVersionList(appVersion);
        return getDataTable(list);
    }

    /**
     * 导出脚本版本列表
     */
    @RequiresPermissions("appversion:version:export")
    @Log(title = "脚本版本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AppVersion appVersion) {
        List<AppVersion> list = appVersionService.selectAppVersionList(appVersion);
        ExcelUtil<AppVersion> util = new ExcelUtil<AppVersion>(AppVersion.class);
        return util.exportExcel(list, "脚本版本数据");
    }

    /**
     * 新增脚本版本
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存脚本版本
     */
    @RequiresPermissions("appversion:version:add")
    @Log(title = "脚本版本", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AppVersion appVersion) {
        long ts = System.currentTimeMillis() / 1000L;
        appVersion.setTs(ts);
        return toAjax(appVersionService.insertAppVersion(appVersion));
    }

    /**
     * 修改脚本版本
     */
    @RequiresPermissions("appversion:version:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        AppVersion appVersion = appVersionService.selectAppVersionById(id);
        mmap.put("appVersion", appVersion);
        return prefix + "/edit";
    }

    /**
     * 修改保存脚本版本
     */
    @RequiresPermissions("appversion:version:edit")
    @Log(title = "脚本版本", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AppVersion appVersion) {
        long ts = System.currentTimeMillis() / 1000L;
        appVersion.setTs(ts);
        return toAjax(appVersionService.updateAppVersion(appVersion));
    }

    /**
     * 删除脚本版本
     */
    @RequiresPermissions("appversion:version:remove")
    @Log(title = "脚本版本", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(appVersionService.deleteAppVersionByIds(ids));
    }
}
